from flask import Flask, request, jsonify

app = Flask(__name__)


def is_logged_in():
    # 这里可以检查 session、token 或其他登录状态
    # 假设请求头中有 `Authorization: Bearer <token>`
    token = request.headers.get('Authorization')
    return token == 'Bearer valid_token'


@app.before_request
def check_login():
    if request.path.startswith('/api') and not is_logged_in():
        return jsonify(error="未认证, 请登录"), 401


@app.route('/')
def Test():
    return 'test'


@app.route('/api/json')
def TestJson():
    return jsonify(message="Hello, World!")


@app.route('/health', methods=['POST'])
def health():
    return jsonify(message="OK")


if __name__ == '__main__':
    app.run(debug=True, port=5000, host='0.0.0.0')
